from lin_base import *
from def_color import *

def add_lin_jieqi(paper,title,x0,y0,xend,font=unicode_font_36,
                    fgcolor=BLACK,lw=2):
    ecl_ras = g_share.ecl_ras
    layer=paper.add_layer(name='jieqi')
    draw = layer.draw
    draw.text((x0-XM_TITLE,y0+YM_TEXT),title, font=font,fill=fgcolor)
    
    jqlist0=[]
    jqlist1=[]
    for j in range(360):
        ra = ecl_ras[j]
        #print(j,ra)
        if (j % 15)> 0:
            continue
        i = int(j / 15)
        ra_hr = ra/15
        xs=RA_to_xs(x0,xend, ra_hr)
        jq = JIE_QIS[i]
        jqcolor = COLOR_JIEQI[jq]
        print('jq:%s %d' % (jq, len(xs)))
        if len(xs) >1:
            jqlist0.append((xs[0],jq,jqcolor))
            jqlist1.append((xs[1],jq,jqcolor))
        else:
            if len(jqlist1)==0 or xs[0] > jqlist1[-1][0]:
                jqlist1.append((xs[0],jq,jqcolor))
            else:
                jqlist0.append((xs[0],jq,jqcolor))
     
    jqlist= jqlist0 + jqlist1
    for i in range(len(jqlist)-1):
        x, jq, jqcolor= jqlist[i]
        x1, _, _ = jqlist[i+1]
        draw.rectangle((x,y0, x1, y0+60), fill=jqcolor)
        draw.line([(x,y0),(x,y0+YM_LONG)], fill=fgcolor, width=lw)
        draw.text((x,y0+YM_TEXT),jq, font=font,fill=fgcolor)
    
    
            
    draw.line([(x0,y0),(xend,y0)],fill=BLACK,width=2)